package com.ha.auto_skip.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import com.ha.auto_skip.R;
import com.ha.auto_skip.bean.JumpBean;
import com.ha.auto_skip.databinding.DialogInputBinding;
import com.xinkao.skmvp.utils.MyToast;

public class InputDialog extends Dialog {
    public InputDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    public static interface OnClickListener {
        /**
         * 点击按钮
         *
         * @param key 关键词
         */
        void onClick(String key, boolean fuzzy);
    }

    public static class Builder {
        private InputDialog dialog;

        private DialogInputBinding binding;

        private Boolean mAutoCancel = true;

        public Builder(Context context) {
            dialog = new InputDialog(context, R.style.ActionTransparentDialogStyle);

            binding = DialogInputBinding.inflate(LayoutInflater.from(context));

            dialog.addContentView(binding.getRoot(),
                    new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT));

            dialog.setCancelable(false);
        }

        public Builder setData(JumpBean bean) {
            binding.title.setText(bean.getLabel());
            binding.key.setText(bean.getKey() != null ? bean.getKey() : "");
            return this;
        }

        /**
         * 设置是否自动调用dismiss
         * @param autoCancel 自动关闭
         * @return this
         */
        public Builder setAutoCancel(Boolean autoCancel) {
            mAutoCancel = autoCancel;
            return this;
        }

        public Builder setPositiveButton(String title, OnClickListener listener) {
            binding.positive.setText(title);
            binding.positive.setOnClickListener(v -> {

                if (TextUtils.isEmpty(binding.key.getText().toString())) {
                    MyToast.warn("请输入关键词");
                    return;
                }

                if (listener != null) {
                    if (mAutoCancel) {
                        dismiss();
                    }
                    listener.onClick(binding.key.getText().toString(), binding.likeCheck.isChecked());
                } else {
                    dismiss();
                }
            });

            return this;
        }

        public Builder setNegativeButton(String title, OnClickListener listener) {
            binding.negative.setVisibility(View.VISIBLE);
            binding.negative.setText(title);
            binding.negative.setOnClickListener(v -> {
                dismiss();
            });

            return this;
        }

        public Builder setOnDismissListener(OnDismissListener listener) {
            dialog.setOnDismissListener(listener);

            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            dialog.setCancelable(cancelable);
            return this;
        }

        public boolean isShowing() {
            return dialog.isShowing();
        }

        public void show() {
            if (!dialog.isShowing()) {
                dialog.show();
            }
        }

        public void dismiss() {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}